<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>装饰器典型应用之表单验证</title>
</head>
<body>
    密码<input id="password"  name="password" text='text'/>
    <button id="register">注册</button>
    <script>
        //通过装饰器模式去验证码
        function registerFn() { 
            alert('提交成功！');
         };
          Function.prototype.before = function (beforeFn) { 
                let thisFn = this;
                return function () { 
                    let pass = beforeFn();
                    if(pass){
                        thisFn.apply(this,arguments);
                    }
                 }
           };

           registerFn = registerFn.before(function () { 
               let pwdValue = document.getElementById('password').value;
               if(!pwdValue) {
                    alert('密码未填写')
                    return false
               };
               return true
            });
            document.getElementById('register').addEventListener('click',registerFn);

    </script>
    
</body>
</html>